package com.example.patrick.readtxtfile;

import android.content.Context;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;

/**
 * Created by Patrick on 25.11.2014.
 */
public class TextFileIO {

    void readTextFile(File FileName, ArrayList <String> pStringArray){
        StringBuilder text = new StringBuilder();

        try {
            BufferedReader br = new BufferedReader(new FileReader(FileName));
            String line;

            while ((line = br.readLine()) != null) {
                pStringArray.add(line);
                text.append(line);
                text.append('\n');
            }
        }
        catch (IOException e) {
            //You'll need to add proper error handling here
        }
    }
    void writeStringToTxt(String string, File FileName,Context myContext){
        try {
            FileName.createNewFile();
            FileOutputStream fOut = new FileOutputStream(FileName);
            OutputStreamWriter myOutWriter =
                    new OutputStreamWriter(fOut);
            myOutWriter.append(string);
            myOutWriter.close();
            fOut.close();
            Toast.makeText(myContext,
                    "Done writing 'Hallo.txt'",
                    Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            Toast.makeText(myContext, e.getMessage(),
                    Toast.LENGTH_SHORT).show();
        }
    }


}
